package com.hbqyml.web.controller.system;

import com.hbqyml.common.annotation.Log;
import com.hbqyml.common.core.controller.BaseController;
import com.hbqyml.common.core.domain.AjaxResult;
import com.hbqyml.common.core.page.TableDataInfo;
import com.hbqyml.common.enums.BusinessType;
import com.hbqyml.common.exception.PojoException;
import com.hbqyml.common.utils.poi.ExcelUtil;
import com.hbqyml.system.domain.Enterprise;
import com.hbqyml.system.domain.vo.EnterpriseVo;
import com.hbqyml.system.service.IEnterpriseService;
import com.hbqyml.system.service.IHbClassifyService;
import com.hbqyml.system.service.IHbRegionService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 企业信息Controller
 * 
 * @author houyutong
 * @date 2022-03-22
 */
@RestController
@RequestMapping("/system/enterprise")
@Api("企业信息控制层")
public class EnterpriseController extends BaseController
{
    @Autowired
    private IEnterpriseService enterpriseService;

    @Autowired
    private IHbClassifyService hbClassifyService;

    @Autowired
    private IHbRegionService hbRegionService;

    /**
     * 查询企业信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:enterprise:list')")
    @GetMapping("/list")
    public TableDataInfo list(Enterprise enterprise)
    {
        startPage();
        List<Enterprise> list = enterpriseService.selectEnterpriseList(enterprise);
        return getDataTable(list);
    }

    /**
     * 导出企业信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:enterprise:export')")
    @Log(title = "企业信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Enterprise enterprise)
    {
        List<Enterprise> list = enterpriseService.selectEnterpriseList(enterprise);
        ExcelUtil<Enterprise> util = new ExcelUtil<Enterprise>(Enterprise.class);
        util.exportExcel(response, list, "企业信息数据");
    }

    /**
     * 获取企业信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:enterprise:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(enterpriseService.selectEnterpriseById(id));
    }

    /**
     * 新增企业信息
     */
    @PreAuthorize("@ss.hasPermi('system:enterprise:add')")
    @Log(title = "企业信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Valid EnterpriseVo enterprise)
    {
        return toAjax(enterpriseService.insertEnterprise(enterprise));
    }

    /**
     * 修改企业信息
     */
    @PreAuthorize("@ss.hasPermi('system:enterprise:edit')")
    @Log(title = "企业信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Valid EnterpriseVo enterprise)
    {
        if (enterprise.getId() == null || enterprise.getId().equals("")) {
            throw new PojoException("缺少id");
        }
        return toAjax(enterpriseService.updateEnterprise(enterprise));
    }

    /**
     * 删除企业信息
     */
    @PreAuthorize("@ss.hasPermi('system:enterprise:remove')")
    @Log(title = "企业信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(enterpriseService.deleteEnterpriseByIds(ids));
    }

    /**
     * 获取所有分类
     * @param search 类型名
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:enterprise:getAllClassify')")
    @GetMapping("/getAllClassify")
    public AjaxResult getAllClassify(@RequestParam(value = "search" ,required = false) String search) {
        return AjaxResult.success(hbClassifyService.getAllClassify(search));
    }

    /**
     * 根据地区id获取相应信息
     * @param regionId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:enterprise:getAListOfRegions')")
    @GetMapping("/getAListOfRegions")
    public AjaxResult getAListOfRegions(@RequestParam(value = "regionId",required = false)Integer regionId) {
        return AjaxResult.success(hbRegionService.getAListOfRegions(regionId));
    }

}
